package com.huakang.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.huakang.entity.Asset;
import com.huakang.service.AssetService;
import com.huakang.utils.AssetPatientVo;
import com.huakang.utils.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/asset")
public class AssetController {
    @Autowired
    private AssetService assetService;

    @GetMapping("/findAll")
    public ResultVo findAll(@RequestParam(defaultValue = "1")  Integer pageNum, @RequestParam(defaultValue = "5") Integer pageSize){
        PageHelper.startPage(pageNum,pageSize);
        List<AssetPatientVo> assetPatientVoList = assetService.findAll();
        PageInfo<AssetPatientVo> pageInfo = new PageInfo<>(assetPatientVoList);
        return ResultVo.success("ok",pageInfo);
    }

    @PostMapping("/assetSearch")
    public ResultVo assetSearch(@RequestBody AssetPatientVo assetPatientVo, @RequestParam(defaultValue = "1")  Integer pageNum, @RequestParam(defaultValue = "5") Integer pageSize){
        PageHelper.startPage(pageNum,pageSize);
        List<AssetPatientVo> assetPatientVoList = assetService.assetSearch(assetPatientVo);
        PageInfo<AssetPatientVo> pageInfo = new PageInfo<>(assetPatientVoList);
        return ResultVo.success("ok",pageInfo);
    }

    @GetMapping("/updateIn")
    public ResultVo updateIn(Integer pid,Double cost){
        Asset asset=assetService.findPid(pid);
        if(asset==null){
            asset=new Asset(null,null,null,null,cost,null,pid);
            boolean b=assetService.insert(asset);
            if(b){
                return ResultVo.success("成功");
            }else{
                return ResultVo.fail("失败");
            }
        }
        asset.setStay(cost);
        boolean b=assetService.update(asset);
        if(b){
            return ResultVo.success("成功");
        }
        return  ResultVo.fail("失败");
    }
    @GetMapping("/updateMe")
    public ResultVo updateMe(Integer pid,Double cost) {
        Asset asset = assetService.findPid(pid);
        if (asset == null) {
            asset = new Asset(null, null, null, null, null, cost, pid);
            boolean b = assetService.insert(asset);
            if (b) {
                return ResultVo.success("成功");
            } else {
                return ResultVo.fail("失败");
            }
        }
        asset.setMedic(cost + asset.getMedic());
        boolean b = assetService.update(asset);
        if (b) {
            return ResultVo.success("成功");
        }
        return ResultVo.fail("失败");
    }

}
